package com.encyclopedia.service.impl;

import com.encyclopedia.domain.PlantCrop;
import com.encyclopedia.domain.PlantCropRelation;
import com.encyclopedia.domain.vo.PlantCropRelationVo;
import com.encyclopedia.mapper.AdminPlantMapper;
import com.encyclopedia.mapper.PlantCropMapper;
import com.encyclopedia.mapper.PlantCropRelationMapper;
import com.encyclopedia.result.exception.ServiceException;
import com.encyclopedia.result.exenum.RequestException;
import com.encyclopedia.service.PlantCropRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-05-03 16:12
 */
@Service
@Slf4j
public class PlantCropRelationServiceImpl implements PlantCropRelationService {

    @Autowired
    private PlantCropRelationMapper plantCropRelationMapper;

    @Autowired
    private PlantCropMapper plantCropMapper;

    @Autowired
    private AdminPlantMapper adminPlantMapper;


    @Override
    public List<PlantCropRelationVo> getInfo(PlantCropRelationVo plantCropRelationVo) {
        List<PlantCropRelationVo> plantCropRelationList = plantCropRelationMapper.getInfo(plantCropRelationVo);
        log.info("查询结果：" + plantCropRelationList);
        return plantCropRelationList;
    }

    @Override
    public List<PlantCrop> getCropInfo(PlantCrop plantCrop) {
        return plantCropMapper.getCropInfo(plantCrop);
    }

    @Override
    public int insertCropRelation(PlantCropRelation plantCropRelation) {
        int column = plantCropRelationMapper.insert(plantCropRelation);
        if (column < 1) {
            throw new ServiceException(RequestException.DELETE_ERROR);
        }
        return column;
    }

    @Override
    public int deleteCropRelation(Integer id) {
        String commonName = plantCropRelationMapper.selectCropRelation(id);
        int i = adminPlantMapper.selectCommonName(commonName);
        if (i == 0) {
            return plantCropRelationMapper.deleterCropRelation(id);
        }
        return 0;
    }

    @Override
    public PlantCropRelationVo selectCropRelation(Integer id) {
        PlantCropRelationVo plantCropRelationVo = plantCropRelationMapper.selectInfo(id);
        return plantCropRelationVo;
    }

    @Override
    public int updateCropRelation(PlantCropRelation plantCropRelation) {
        plantCropRelationMapper.updatePlantCropRelation(plantCropRelation);
        return 0;
    }
}
